package com.hunter.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class ParamMd5Sig{
	
	public static String generateSig(Map<String,String> map, String sec){
		List<String> list = new ArrayList<String>();
		for(Map.Entry<String, String> entry : map.entrySet()){
			list.add(entry.getKey());
		}
		Collections.sort(list);
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for(String key : list){
			if(i++ != 0){
				sb.append("&");
			}
			sb.append(key).append("=").append(map.get(key));
		}
//		System.out.println(sb.toString());
//		System.out.println(sb.toString()+(sec==null?"":sec));
//		System.out.println(MD5Util.md5(sb.toString()+(sec==null?"":sec)));
		return MD5Util.md5(sb.toString()+(sec==null?"":sec));
	}
	
	public static String generateSigByValue(Map<String,String> map,String sec){
		List<String> list = new ArrayList<String>();
		for(Map.Entry<String, String> entry : map.entrySet()){
			list.add(entry.getKey());
		}
		Collections.sort(list);
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for(String key : list){
			if(StringUtils.isNotBlank(map.get(key))){
				if(i != 0){
					sb.append("_");
				}
				sb.append(map.get(key));
				i++;
			}
		}
		return MD5Util.md5(sb.toString()+(sec==null?"":sec));
	}
	
	
	public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("realname","戴圣立");
		map.put("idType","1");
		map.put("idCard","330382199509026958");
		map.put("idFront","D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file/20170719/17/201707191718170542.jpg");
		map.put("idBack","D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file/20170719/17/201707191718180717.jpg");
		map.put("videoInfo","D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file/20170719/17/201707191718470901.mp4");
		map.put("fingerprintImg","D:\\develop\\apache-tomcat-7.0.62\\webapps\\skyeye-file/20170719/17/201707191718280811.bmp");
//		map.put("fingerprintVerify",null);
		map.put("flagtime","1500455932724");// sig=d825133fb1a04b8a1662d99398ff65c5"
		System.out.println(generateSig(map, "sigkey654321"));
		System.out.println((System.currentTimeMillis()-1500455932724l)/1000/60);
	}
}
